/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package justdoit.view;

import javax.swing.JButton;
import justdoit.Startup;
import justdoit.actions.NextTaskAction;
import justdoit.actions.PreviousTaskAction;
import justdoit.controller.TaskManager;
import justdoit.model.Task;
import justdoit.controller.LanguageManager;

/**
 * The TaskDialog for Editing a Existing Task. Inherited from TaskDialog.
 * @author Can
 */
public class EditTaskDialog extends TaskDialog {

    private Task task;
    private JButton nextButton;
    private JButton previousButton;

    public EditTaskDialog(Task task) {
        super(LanguageManager.getString("title.editTask"));
        this.task = task;
        updateInterfacebyTask(task);
        this.initComponents();
    }

    private void initComponents() {
        nextButton = new JButton(new NextTaskAction(this));
        previousButton = new JButton(new PreviousTaskAction(this));
        insertToActionPanel(nextButton, 0);
        insertToActionPanel(previousButton, 0);
    }

    public void update(Task task) {
        this.task = task;
        updateInterfacebyTask(task);
    }

    @Override
    protected void okAction() {
        if (validation() < 0) {
            return;
        }
        getTaskFromInterface(this.task);
        if (TaskManager.updateTask(task) == -1) {
            System.out.println("Update failed!");
        } else {
            Startup.getMainFrame().getTaskListPanel().refreshData();
        }
        super.okAction();
    }
}
